﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace JSZJDataProSys
{
    public partial class FrmDataConvert : Form
    {

        /// <summary>
        /// 原始坐标文件名称
        /// </summary>
        private string sourceFileName = "";

        /// <summary>
        /// 转换后的文件名称
        /// </summary>
        private string TargetFileName = "";

        public FrmDataConvert()
        {
            InitializeComponent();
        }


        private void btnTransfer_Click(object sender, EventArgs e)
        {
            //数据转换系统
            try
            {
                if (sourceFileName == "")
                {
                    MessageBox.Show("请选择原始坐标文件，文件后缀名为dat、txt、asc", "选择文件");
                    return;
                }
                if (TargetFileName == "")
                {
                    MessageBox.Show("请选择转换数据保存路径，文件后缀名为dat、txt、asc", "选择文件");
                    return;
                }
                List<string> result = CoreLib.TrimbleFileConvertClass.ConvertFromASC(sourceFileName);
                if (CoreLib.SaveFileClass.SaveASCIIFile(TargetFileName, result) == true)
                {
                    MessageBox.Show("转换成功，共处理 " + result.Count + " 条数据", "信息提示");
                    sourceFileName = "";
                    TargetFileName = "";

                    txtSourceFile.Text = "";
                    txtTargetFile.Text = "";
                }
            }
            catch (Exception ex)
            {
                //抛出异常，
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSelSourceFile_Click(object sender, EventArgs e)
        {
            //选择目标文件
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "二进制文件(*.dat;*.asc;*.txt)|*.dat;*.asc;*.txt";
            ofd.FilterIndex = 1;
            ofd.RestoreDirectory = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                sourceFileName = ofd.FileName;
                txtSourceFile.Text = sourceFileName;
            }

        }

        private void btnSelTargetFile_Click(object sender, EventArgs e)
        {
            //选择保存文件路径            
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = " DAT files(*.dat)|*.dat|ASC files(*.asc)|*.asc|Txt files(*.txt)|*.txt";
            sfd.FilterIndex = 1;
            if (sourceFileName != "")
            {
                string sourceFilePath =Path.GetFullPath(sourceFileName);
                sfd.InitialDirectory = sourceFilePath;
                sfd.FileName = Path.GetFileNameWithoutExtension(sourceFileName) + "_transferred" + Path.GetExtension(sourceFileName);
            }
            sfd.RestoreDirectory = true;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                TargetFileName = sfd.FileName;
                txtTargetFile.Text = TargetFileName;
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}